package com.cg.controller;


import com.cg.common.Result;
import com.cg.pojo.entity.EventEntity;
import com.cg.pojo.entity.UserEntity;
import com.cg.service.EventService;
import com.cg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/event")
@CrossOrigin
public class EventController {

    @Autowired
    private EventService eventService;

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public Result<String> add(@RequestBody EventEntity event){
        // 根据用户名获取用户id
        UserEntity user = userService.getUserByUsername(event.getUsername());
        event.setUserId(user.getId());
        event.setStatus(event.getStatus() != null ? event.getStatus() : "未完成");
        event.setLevel(event.getLevel() != null ? event.getLevel() : "重要");
        event.setStartDate(event.getStartDate());
        //打印添加的事件信息
        System.out.println("event: " + event);

        eventService.add(event);

        return Result.succsee("添加成功");
    }
    @RequestMapping("/update")
    public Result<String> update(@RequestBody EventEntity event){
        eventService.update(event);
        return Result.succsee("更新成功");
    }
    @RequestMapping("/updateStatus_zf/{eventId}")
    public Result<String> updateStatus_zf(@PathVariable("eventId") Integer eventId){
        eventService.updateStatus_zf(eventId);
        return Result.succsee("更新作废状态成功");
    }

    @RequestMapping("/updateStatus")
    public Result<String> updateStatus(@RequestParam(value = "eventId") Integer eventId, @RequestParam(value = "status") String status){
        eventService.updateStatus(eventId,status);
        return Result.succsee("更新状态成功");
    }

    @RequestMapping("/delete/{eventId}")
    public Result<String> delete(@PathVariable("eventId") Integer eventId){
        eventService.delete(eventId);
        return Result.succsee("删除成功");
    }
///event/listByUser/14
@RequestMapping("/listByUser/{userId}")
public Result<List<EventEntity>> listByUser(@PathVariable("userId") Integer userId){
    List<EventEntity> events = eventService.listByUser(userId);
    events.sort((e1, e2) -> e2.getEndDate().compareTo(e1.getEndDate()));
    //整齐分布打印事件列表中的数据
    for (EventEntity event : events) {
        System.out.println("event: " + event);
        System.out.println();
    }
    System.out.println("==============================");

    return Result.succsee(events);
}
    @RequestMapping("/get/{eventId}")
    public Result<EventEntity> get(@PathVariable("eventId") Integer eventId){
        EventEntity event = eventService.getEventById(eventId);
        return Result.succsee(event);
    }
    @RequestMapping("/search")
    public Result<List<EventEntity>> search(
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "categoryId", required = false) Integer categoryId,
            @RequestParam(value = "level", required = false) String level,
            @RequestParam(value = "status", required = false) String status) {

        List<EventEntity> events = eventService.searchEvents(userId, title, categoryId, level, status);
        events.sort((e1, e2) -> e2.getEndDate().compareTo(e1.getEndDate()));
        //整齐分布打印事件列表中的数据
        for (EventEntity event : events) {
            System.out.println("event: " + event);
        }
        System.out.println("==============================");
        return Result.succsee(events);
    }


}
